//
//  ViewController.swift
//  逐帧动画
//
//  Created by app on 2017/4/13.
//  Copyright © 2017年 app. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var imageView:UIImageView!
    var timer:Timer?
    var index:Int = 0
    var displayLink:CADisplayLink!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        imageView = UIImageView()
        imageView.frame = UIScreen.main.bounds
        imageView.contentMode = .scaleAspectFit
        
        view.addSubview(imageView)
        
//        index = 0
//        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(refushImage), userInfo: nil, repeats: true)
        
        displayLink = CADisplayLink.init(target: self, selector: #selector(refushImage))
        displayLink.preferredFramesPerSecond = 1
        displayLink.add(to: .current, forMode: .defaultRunLoopMode)
    }

    
    func refushImage() {
        imageView.image = UIImage(named: "\(index).png")
        index += 1
        if index == 67 {
         timer?.invalidate()
            index -= 1
            imageView.image = UIImage(named: "\(index).png")
        }
    }

}

